home *** CD-ROM | disk | FTP | other *** search
- /* pedigree: show process ancestry
- * Eric P. Scott, San Francisco State University, July 1993
- * added -h switch -- Bruce Gingery bruce@TotSysSoft.com Wed Aug 4 03:17:30 MDT 1993
-
- *
- * Compile on 2.x:
- * cc -o pedigree -s -object -O -bsd pedigree.c
- * Compile on 3.0:
- * cc -o pedigree -s -object -O -bsd -DNX_COMPILER_RELEASE_3_0 pedigree.c
- * Compile on 3.1:
- * cc -o pedigree -s -object -O2 -bsd -fno-builtin pedigree.c
- */
- #include <stdio.h>
- #include <sys/errno.h>
- #ifdef __STRICT_BSD__
- #include <strings.h>
- #else
- #include <string.h>
- #endif
- #include <sys/types.h>
- #include <sys/dir.h>
- #include <sys/stat.h>
- #ifndef NX_COMPILER_RELEASE_3_0
- #include <sys/table.h>
- #else
- /*
- * Mach Operating System
- * Copyright (c) 1986 Carnegie-Mellon University
- * All rights reserved. The CMU software License Agreement specifies
- * the terms and conditions for use and redistribution.
- */
- #define TBL_PROCINFO 10 /* index by proc table slot */
- /*
- * TBL_PROCINFO data layout
- */
- #define PI_COMLEN 19 /* length of command string */
- struct tbl_procinfo
- {
- int pi_uid; /* user ID */
- int pi_pid; /* proc ID */
- int pi_ppid; /* parent proc ID */
- int pi_pgrp; /* proc group ID */
- int pi_ttyd; /* controlling terminal number */
- int pi_status; /* process status: */
- #define PI_EMPTY 0 /* no process */
- #define PI_ACTIVE 1 /* active process */
- #define PI_EXITING 2 /* exiting */
- #define PI_ZOMBIE 3 /* zombie */
- int pi_flag; /* other random flags */
- char pi_comm[PI_COMLEN+1];
- /* short command name */
- };
- #endif
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- extern int errno;
- int chrdev();
- register struct direct *d, **dp;
- register int i;
- int pid, n;
- struct tbl_procinfo pi;
- struct direct **df;
- dev_t lastd;
- char tty[4];
-
- if (argc<1||argc>2) {
- (void)fprintf(stderr, "Usage: %s [pid]\n", *argv);
- exit(1);
- }
- if (argc==2) {
- if (!strcmp(argv[1],"-h")) {
- (void)fprintf(stderr, "Usage: %s [pid]\n", *argv);
- exit(1);
- }
- pid=atoi(argv[1]);
- if (kill(pid, 0)<0&&errno!=EPERM) {
- perror(argv[1]);
- exit(1);
- }
- }
- else pid=getpid();
- if (chdir("/dev")<0) {
- perror("chdir");
- exit(1);
- }
- if ((n=scandir(".", &df, chrdev, alphasort))<0) {
- (void)fputs("scandir() failed\n", stderr);
- exit(1);
- }
- lastd=(dev_t)-1;
- tty[0]='?', tty[1]='\0'; tty[2]='\0', tty[3]='\0';
- (void)fputs(" UID PID PPID PGRP TT COMMAND\n", stdout);
- for (;;) {
- if (table(TBL_PROCINFO, pid, (char *)&pi, 1,
- sizeof pi)==1&&pi.pi_status==PI_ACTIVE) {
- if (pi.pi_ttyd!=lastd) {
- if (pi.pi_ttyd<0) tty[0]='?', tty[1]='\0';
- else {
- dp=df;
- i=n; do {
- d= *dp++;
- if (*(dev_t *)&d->d_ino==(dev_t)pi.pi_ttyd) {
- if (d->d_namlen>3&&!strncmp(d->d_name, "tty", 3))
- (void)strncpy(tty, d->d_name+3, 3);
- else {
- tty[0]=d->d_name[0], tty[1]=d->d_name[1];
- tty[2]='\0';
- }
- goto x;
- }
- } while (--i>0);
- tty[0]='?', tty[1]='?'; tty[2]='\0';
- }
- x:
- lastd=pi.pi_ttyd;
- }
- (void)printf("%5d %5d %5d %5d %-3s%.*s\n", pi.pi_uid,
- pid, pi.pi_ppid, pi.pi_pgrp, tty,
- PI_COMLEN, pi.pi_comm);
- }
- else {
- (void)fputs("something went wrong\n", stderr);
- exit(1);
- }
- if (pid<=1) break;
- pid=pi.pi_ppid;
- }
- exit(0);
- }
-
- int chrdev(d)
- struct direct *d;
- {
- struct stat st;
-
- if (d->d_name[0]=='t'||(d->d_name[0]=='c'&&d->d_name[1]=='o')) {
- d->d_name[d->d_namlen]='\0';
- if (stat(d->d_name, &st)>=0&&(st.st_mode&S_IFMT)==S_IFCHR) {
- *(dev_t *)&d->d_ino=st.st_rdev;
- return(1);
- }
- }
- return(0);
- }
-